package org.example.mybatisdemo.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatisdemo.model.UserInfo;

import java.util.List;
@Mapper
public interface UserInfoMapper {

    //查询所有用户
    @Select("select * from userinfo")
    public List<UserInfo> queryAllUser();

    //查找 id=4 的用户
    @Select(("select * from userinfo where id = #{id}"))
    public UserInfo queryUserById(Integer id);

    //使用别名查找 id=4 的用户
    //@Param()中的值要和 #{}中的一致
    @Select(("select * from userinfo where id = #{userid}"))
    public UserInfo queryUserById_Rename(@Param("userid") Integer id);

    //增加一条语句
    //默认返回的是 Integer类型 否则会报错
    // 想要拿到自增id 需要加上 @Options注解
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) values " +
            "(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    public Integer insertUserinfo(@Param("userInfo") UserInfo userinfo);

//    @ResultMap("BaseMap")
//    @Select("select * from userinfo")
//    List<UserInfo> queryUserInfo();
//
//    @Select("select * from userinfo where id = #{id}")
//    UserInfo queryUserInfoById(@Param("id") Integer id);
//
//    @Select("select * from userinfo where id = #{id} and name = #{name}")
//    UserInfo queryUserInfoByName(@Param("id") Integer id, @Param("name") String name);
//
//    @Insert("insert into userinfo (username,`password`,age,gender,phone) " +
//            "values (#{username},#{password},#{age},#{gender},#{phone})")
//    Integer insert(UserInfo userInfo);
//
////    @Options(useGeneratedKeys = true, keyProperty = "id")
////    @Insert("insert into userinfo (username, age, gender, phone) " +
////            "values (# {userinfo.username},#{userinfo.age},#{userinfo.gender},#{userinfo.phone})")
////    Integer insert2(UserInfo userInfo);
//
//    @Options(useGeneratedKeys = true, keyProperty = "id")
//    @Insert("insert into userinfo (username, `password`, age, gender, phone, delete_flag)" +
//            " values(#{userInfo.username},#{userInfo.password}," +
//            "#{userInfo.age},#{userInfo.gender},#{userInfo.phone}, #{userInfo.deleteFlag})")
//    Integer insert2(@Param("userInfo") UserInfo userInfo);
//
//
//    //1、起别名来映射
//    @Select("select id, username, password, age, gender, phone, " +
//            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime" +
//            "from userinfo")
//    List<UserInfo> queryUserInfoRename();
//
//    //2、使用注解来映射
//    @Select("select * from userinfo")
//    @Results({
//            @Result(column = "delete_flag",property = "deleteFlag"),
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime")
//    })
//    List<UserInfo> queryUserInfoRename2();
//
//    //3、可以多次利用 使用@ResultMap注解来复用映射关系
//    @Select("select * from userinfo")
//    @Results(id = "BaseMap",value = {
//            @Result(column = "delete_flag",property = "deleteFlag"),
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime")
//    })
//    List<UserInfo> queryUserInfoRename3();
//
//    //4、


}
